<template>
    <mescroll-uni
        ref="mescrollRef"
        @init="mescrollInit"
        :height="height"
        :down="downOption"
        @down="refresh"
        :up="upOption"
        @up="getData">
        <coupon-list-item
            v-for="(item, index) in datas"
            :showSelect="showSelect"
            :selectId="selectId"
            :key="index"
            :item="item"
            :state="data.state"
            @selectFn="selectFn"></coupon-list-item>
    </mescroll-uni>
</template>
<script>
import couponListItem from "@/pages_shop/components/coupon/coupon-list-item.vue"
import { getUserCouponDetailPage } from "@/service/api/coupon.js"

export default {
    props: {
        height: {
            type: String,
            default: "",
        },
        data: {
            type: Object,
            default: () => {},
        },
        showSelect: {
            type: Boolean,
            default: false,
        },
        selectId: {
            type: String,
            default: "",
        },
        coupons: {
            type: Array,
            default: null,
        },
    },
    components: {
        couponListItem,
    },
    data() {
        return {
            datas: [],
            productList: [],
            param: {
                state: 0, //0 已领取待使用，有可能还没开始；1 已使用； 2未使用已过期；3已领取待使用，在有效期内；4赠送；
                privilegeWay: -1, //0 折扣券；1抵用券；2随机抵用券；3动态抵用券；-1所有
            },
        }
    },
    mounted() {
        this.param.state = this.data.state
        this.$nextTick(() => {
            this.refresh()
        })
    },
    methods: {
        getData(page) {
            if (this.coupons) {
                this.mescroll.endBySize(this.coupons.length, this.coupons.length)
                this.datas = this.datas.concat(this.coupons)
            } else {
                let param = this.param
                param.state = this.data.state
                param.PageIndex = page.num
                param.PageSize = page.size

                getUserCouponDetailPage(param).then((res) => {
                    if (res.Data && res.Data.Data) {
                        this.mescroll.endBySize(res.Data.Data.length, res.Data.RecordAmout)
                        this.datas = this.datas.concat(res.Data.Data)
                    } else {
                        this.mescroll.endBySize(0, 0)
                    }
                })
            }
        },
        refresh() {
            this.datas = []
            this.mescroll.resetUpScroll()
        },
        selectFn(id) {
            let selectObj = this.datas.find((e) => {
                return String(e.ID) == id
            })
            this.$emit("selectFn", selectObj)
        },
    },
}
</script>

<style lang="scss"></style>
